#include "main.h"
#include "pwm.h"
#include "type.h"

static void time2_base_init(void)
{
    TIM_TimeBaseInitType TIM_TimeBaseStructure;
    TIM_Base_Struct_Initialize(&TIM_TimeBaseStructure);
    TIM_TimeBaseStructure.Period    = 1000-1;
    TIM_TimeBaseStructure.Prescaler = 64-1;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;

    TIM_Base_Initialize(TIM2, &TIM_TimeBaseStructure);  
}

static void time2_pwm_config(void)
{
    OCInitType TIM_OCInitStructure;

    TIM_Output_Channel_Struct_Initialize(&TIM_OCInitStructure);

    /* Channel 1, 2 and 3 Configuration in PWM mode */
    TIM_OCInitStructure.OcMode       = TIM_OCMODE_PWM2;
    TIM_OCInitStructure.OutputState  = TIM_OUTPUT_STATE_ENABLE;
    TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_ENABLE;
    TIM_OCInitStructure.Pulse        = 500;
    TIM_OCInitStructure.OcPolarity   = TIM_OC_POLARITY_LOW;
    TIM_OCInitStructure.OcNPolarity  = TIM_OCN_POLARITY_LOW;
    TIM_OCInitStructure.OcIdleState  = TIM_OC_IDLE_STATE_SET;
    TIM_OCInitStructure.OcNIdleState = TIM_OCN_IDLE_STATE_RESET;


    TIM_Output_Channel3_Initialize(TIM2, &TIM_OCInitStructure);

    // TIM_OCInitStructure.Pulse        = 250;

    // TIM_Output_Channel2_Initialize(TIM2, &TIM_OCInitStructure);

}

static void time2_pwm_gpio_init(void)
{

    GPIO_InitType GPIO_InitStructure;

    GPIO_Structure_Initialize(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Mode  = GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Current = GPIO_DS_4MA;

    /* TIM2 CH3 PA2 pins init */
    GPIO_InitStructure.Pin        = GPIO_PIN_2;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF3_TIM2;
    GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
    
    /* TIM2 CH3 PA1 pins init */
    // GPIO_InitStructure.Pin        = GPIO_PIN_1;
    // GPIO_InitStructure.GPIO_Alternate = GPIO_AF3_TIM2;
    // GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
}

static MI_BOOL time2_pwm_rcc_init(void)
{
    uint32_t ADTIM_clock;

    RCC_ClocksType RCC_Clocks;

    RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA);

    RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_AFIO);
    
    //APB 高速(APB2)预分频器
    //由软件置位和清零，配置 APB2 时钟(PCLK2)的分频系数。需确保 PCLK2 不 超过 64MHz。
    //0xx:HCLK 不分频
    //100:HCLK 2 分频
    //101:HCLK 4 分频
    //110:HCLK 8 分频
    //111:HCLK 16 分频
   //RCC_Pclk1_Config(RCC_HCLK_DIV4);

    RCC_APB1_Peripheral_Clock_Enable(RCC_APB1_PERIPH_TIM2);
   


    return MI_TRUE;
}

static void tim2_interrupt_init(void)
{
    NVIC_InitType NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Initializes(&NVIC_InitStructure);
    TIM_Interrupt_Enable(TIM2, TIM_INT_UPDATE);
}

void tim2_pwm_init(void)
{
    time2_pwm_rcc_init();
    time2_pwm_gpio_init();
    time2_pwm_config();
    tim2_interrupt_init();
    time2_base_init();

    TIM_On(TIM2);
}


